<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery1">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="门店">
              <a-cascader
                :options="departModel.departTreeOptions"
                :showSearch="true"
                expand-trigger="hover"
                placeholder=""
                :fieldNames="departModel.fieldNames"
                style="width: 180px"
                v-model="queryParam.selecteddeparts"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="订单号">
              <a-input placeholder="" v-model="queryParam.orderNo" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="SKU编码">
              <a-input placeholder="" v-model="queryParam.itemSku" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="下单时间">
              <a-range-picker
                :ranges="{ 今天: [moment(), moment()], 本月: [moment().startOf('month'), moment().endOf('month')] }"
                v-model="orderDateRange"
                @change="handleChange"
              />
            </a-form-item>
          </a-col>

          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery1" icon="search">查询</a-button>
              <a-button @click="handleRule" type="primary" icon="setting" style="margin-left: 8px">设置规则</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <!-- <div class="table-operator">
      <a-button @click="handleRule" type="primary" icon="setting">设置规则</a-button>
    </div> -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="pics" slot-scope="text, record">
          <a @click="handleView(text)">查看</a>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
    <j-modal :visible="imageVisible" title="查看" :footer="null" :width="800" @cancel="handleCancel">
      <img :src="imgLink" style="max-width: 750px" />
    </j-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { postAction } from '@/api/manage'
import moment from 'moment'
import { queryDepartTreeList1 } from '@/api/api'

export default {
  name: 'PosCashierDetailAdjustList',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      description: '订单手工优惠明细管理页面',
      departModel: {
        departTreeOptions: [],
        fieldNames: { label: 'title', value: 'value', children: 'children' },
      },
      departTreeOptions: [],
      orderDateRange: [],
      orgIds: '',
      shopList: [],
      imageVisible: false,
      imgLink: '',
      fieldNames: { label: 'title', value: 'value', children: 'children' },
      // 表头
      columns: [
        {
          title: '所属机构',
          align: 'center',
          dataIndex: 'orgName',
        },
        {
          title: '下单门店',
          align: 'center',
          dataIndex: 'shopName',
        },
        {
          title: '订单号',
          align: 'center',
          dataIndex: 'orderNo',
        },
        {
          title: '下单时间',
          align: 'center',
          dataIndex: 'operTime',
        },
        {
          title: '收银员姓名',
          align: 'center',
          dataIndex: 'bizUserName',
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          align: 'center',
        },
        {
          title: 'SKU编码',
          align: 'center',
          dataIndex: 'itemSku',
        },
        {
          title: '商品名称',
          align: 'center',
          dataIndex: 'itemSkuNm',
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'cnt',
        },
        {
          title: '商品销售价',
          align: 'center',
          dataIndex: 'salePrice',
        },
        {
          title: '商品活动优惠',
          align: 'center',
          dataIndex: 'disPrice',
        },
        {
          title: '手工调整金额',
          align: 'center',
          dataIndex: 'adjustAmount',
        },
        {
          title: '实付金额',
          align: 'center',
          dataIndex: 'payPrice',
        },
        {
          title: '调整原因',
          align: 'center',
          dataIndex: 'remarks',
        },
        {
          title: '图片',
          align: 'center',
          dataIndex: 'pics',
          scopedSlots: { customRender: 'pics' },
        },
      ],
      url: {
        list: '/cashier/posCashierDetailAdjust/list',
        shopListUrl: '/stock/posShop/shopList',
      },
    }
  },
  computed: {},
  created() {
    this.loadDepartTree()
  },
  methods: {
    moment,
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    searchQuery1() {
      this.assembleQueryParam()
      this.loadData()
    },
    assembleQueryParam() {
      console.log(this.queryParam.selecteddeparts)
      let selecteddeparts = this.queryParam.selecteddeparts
      if (selecteddeparts && selecteddeparts.length == 2) {
        this.queryParam.orgIds = selecteddeparts[0]
        this.queryParam.shopIds = selecteddeparts[1] == 0 ? null : selecteddeparts[1]
      } else {
        this.queryParam.orgIds = null
        this.queryParam.shopIds = null
      }
    },
    loadDepartTree() {
      queryDepartTreeList1().then((res) => {
        if (res.success) {
          let departList = res.result
          if (departList.length == 1 && departList[0].orgType == 1) {
            departList = departList[0].children
          }
          this.departModel.departTreeOptions = departList

          this.departModel.departTreeOptions.forEach((x) => {
            if (!x['children']) {
              x['children'] = []
            }
            let allShop = { title: '全部门店', value: '0' }
            x['children'].unshift(allShop)
          })
        }
      })
    },
    handleRule() {
      this.$router.push({ path: '/order/adjustrule' })
    },
    handleView(imgLink) {
      this.imageVisible = true
      this.imgLink = imgLink
    },
    handleCancel() {
      this.imageVisible = false
      this.imgLink = ''
    },
    handleChange(date, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },
  },
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>